package com.homestay.controller;


import com.homestay.beans.Admins;
import com.homestay.service.AdminsService;
import com.homestay.service.impl.AdminsServiceImpl;
import com.homestay.utils.CookieUtils;
import com.homestay.utils.DateFormatUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/AdminsServlet.do")
public class AdminsServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String action = req.getParameter("action");
        switch (action) {
            case "login":
                login(req, resp);
                break;
            case "logout":
                logout(req, resp);
            default:
                resp.getWriter().write("<script>alert('未知操作');location.href='login.jsp';</script>");
        }

    }



    private void login(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String code = req.getParameter("code");
            HttpSession session = req.getSession();
            // 自动登录
//            username="admin";
//            password="123456";
//            code = (String)session.getAttribute("systemCode");

            if (CodeServlet.isEquals(code, req)) {
                AdminsService adminsService = new AdminsServiceImpl();
                Admins admin = adminsService.Login(username, password);
                if(admin != null) {

                    session.setAttribute("nowUser", username);

                    String time = CookieUtils.getCookieValue(req, "message");
                    if(time.isEmpty()){
                        time = DateFormatUtils.Now();
                        Cookie cookie = new Cookie("message", URLEncoder.encode(time, "UTF-8"));
                        cookie.setMaxAge(60 * 60 * 24 * 7);
                        resp.addCookie(cookie);
                    }else{
                        String newTime = DateFormatUtils.Now();
                        Cookie cookie = new Cookie("message", URLEncoder.encode(newTime, "UTF-8"));
                        cookie.setMaxAge(60 * 60);
                        resp.addCookie(cookie);
                    }
                    session.setAttribute("time", time);
                    resp.sendRedirect("views/index.jsp");
                } else {
                    req.setAttribute("msg", "账号或密码错误");
                    req.getRequestDispatcher("login.jsp").forward(req, resp);
                }
            }else{
                req.setAttribute("msg", "验证码错误");
                req.getRequestDispatcher("login.jsp").forward(req, resp);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) {
        try {
            HttpSession session = req.getSession();
            session.invalidate();
            resp.sendRedirect("login.jsp");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
